package com.ruoyi.project.channel.channelRate.controller;

import java.util.List;

import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.channel.channelRate.domain.ChannelRate;
import com.ruoyi.project.channel.channelRate.service.IChannelRateService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 通道商费率信息操作处理
 *
 * @author zero
 * @date 2019-05-13
 */
@Controller
@RequestMapping("/channel/channelRate")
public class ChannelRateController extends BaseController {
    private String prefix = "channel/channelRate";
    @Autowired
    private IUserService userService;
    @Autowired
    private IChannelRateService channelRateService;

    @RequiresPermissions("channel:channelRate:view")
    @GetMapping("/{channelUserId}")
    public String channelRate(@PathVariable("channelUserId") Long channelUserId, ModelMap mmap) {
        mmap.put("channelUserId", channelUserId);
        return prefix + "/channelRate";
    }

    /**
     * 查询通道商费率列表
     */
    @RequiresPermissions("channel:channelRate:list")
    @PostMapping("/list/{channelUserId}")
    @ResponseBody
    public TableDataInfo list(ChannelRate channelRate,@PathVariable("channelUserId") Long channelUserId) {
        startPage();
        channelRate.setChannelUserId(channelUserId);
        List<ChannelRate> list = channelRateService.selectChannelRateList(channelRate);
        return getDataTable(list);
    }


    /**
     * 导出通道商费率列表
     */
    @RequiresPermissions("channel:channelRate:export")
    @PostMapping("/export/{channelUserId}")
    @ResponseBody
    public AjaxResult export(ChannelRate channelRate,@PathVariable("channelUserId") Long channelUserId) {
        channelRate.setChannelUserId(channelUserId);
        List<ChannelRate> list = channelRateService.selectChannelRateList(channelRate);
        ExcelUtil<ChannelRate> util = new ExcelUtil<ChannelRate>(ChannelRate.class);
        return util.exportExcel(list, "channelRate");
    }

    /**
     * 新增通道商费率
     */
    @GetMapping("/add/{channelUserId}")
    public String add(@PathVariable("channelUserId") Long channelUserId, ModelMap mmap) {
        mmap.put("channelUserId", channelUserId);
        // 获取所有渠道商
        mmap.put("payers", userService.selectPayerlList(new User()));
        return prefix + "/add";
    }

    /**
     * 新增保存通道商费率
     */
    @RequiresPermissions("channel:channelRate:add")
    @Log(title = "通道商费率", businessType = BusinessType.INSERT)
    @PostMapping("/add/{channelUserId}")
    @ResponseBody
    public AjaxResult addSave(ChannelRate channelRate,@PathVariable("channelUserId") Long channelUserId) {
        ChannelRate search = new ChannelRate();
        search.setChannelUserId(channelUserId);
        search.setPayType(channelRate.getPayType());
        List<ChannelRate> rates = channelRateService.selectChannelRateList(search);
        if(rates.size() > 0){
            rates.get(0).setPayRate(channelRate.getPayRate());
            return toAjax(channelRateService.updateChannelRate(rates.get(0)));
        }
        channelRate.setChannelUserId(channelUserId);
        return toAjax(channelRateService.insertChannelRate(channelRate));
    }

    /**
     * 修改通道商费率
     */
    @GetMapping("/edit/{rateId}")
    public String edit(@PathVariable("rateId") Long rateId, ModelMap mmap) {
        ChannelRate channelRate = channelRateService.selectChannelRateById(rateId);
        mmap.put("channelRate", channelRate);
        // 获取所有渠道商
        mmap.put("payers", userService.selectPayerlList(new User()));
        return prefix + "/edit";
    }

    /**
     * 修改保存通道商费率
     */
    @RequiresPermissions("channel:channelRate:edit")
    @Log(title = "通道商费率", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ChannelRate channelRate) {
        return toAjax(channelRateService.updateChannelRate(channelRate));
    }

    /**
     * 删除通道商费率
     */
    @RequiresPermissions("channel:channelRate:remove")
    @Log(title = "通道商费率", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(channelRateService.deleteChannelRateByIds(ids));
    }

}
